function init()
{
	// Place the API id in the text field
	mwl.setInputValue('#forum_id', api.ID.toString(16));
	
	// Create the object to store topics in
	window.topics = {};
	
	// Get the series and title
	api.series.get(onSeries);
	
	// Get the list of discussion topics
	api.topics.list(onTopics);
}

// Callback for series list
function onSeries(data) {
	mwl.insertHTML('#series_title', data.name);
	mwl.show('#series_title');
}

// Callback for topics list
function onTopics(data) {
	if(data.items) {
		var i, item, prev, next, items = '', len = data.items.length;
		for(i = 0; i < len; i++) {
			item = data.items[i];
		
			// Doubly link the items for prev/next
			item.prev = data.items[i === 0 ? len - 1 : i - 1];
			item.next = data.items[i === len - 1 ? 0 : i + 1];
			item.submissions = [];
			
			// Store the item globally for later use
			if(item.id)
				window.topics[item.id.topicId] = item;
			
			// Add the item to the topics list					
			items += template('<li {className} id="item{i}" onclick="{onClick}">{title}</li>', {
					i: i,
					className: i == 0 ? 'class="first"' : "",
					title: item.name + ' ' + item.description,
					onClick: "mwl.switchClass('#topics', 'normal', 'left');"
						+ "mwl.removeClass('#discussion', 'right');"
						+ "mwl.insertHTML('#discussion_title', '" + item.name + "');"
						+ "mwl.insertHTML('#discussion_description', '" + item.description + "');"
						+ "mwl.setInputValue('#current', " + item.id.topicId + ");"
				}
			);
		}
		mwl.insertHTML('#topics_list', items);
	} else {
		mwl.insertHTML('#topics_list', '<li>Could not retrieve topics.</li>');		
	}
}

function prev() {
	var id = document.getElementById('current').value;
	nav(window.topics[id].prev);
}

function next() {
	var id = document.getElementById('current').value;
	nav(window.topics[id].next);
}

function nav(item) {
	mwl.insertHTML('#discussion_title', item.name);
	mwl.insertHTML('#discussion_description', item.description);
	mwl.insertHTML('#submissions', '<li>Loading...</li>');
	mwl.setInputValue('#current', item.id.topicId);
	api.submissions.list(item.id.topicId, onSubmissions);
}

// Callback for topic submissions
function onSubmissions(data) {
	if (data.items) {
		var i, item, items = '';
		for (i = 0; i < data.items.length; i++) {
			item = data.items[i];
			items += template('<li id="submission{i}" class="compact" onclick="{onClick}">'
				+ '<span class="plus">{plus}</span><span class="minus">{minus}</span>'
				+ '{text}<br /><cite>-{attribution}</cite></li>', {
				i: i,
				text: item.text,
				plus: item.counters.plusVotes,
				minus: item.counters.minusVotes,
				attribution: item.attribution.displayName + ' (' + item.attribution.location + ')',
				onClick: "mwl.toggleClass('#submission" + i + "', 'compact', 'expanded');"
			});
		}
		mwl.insertHTML('#submissions', items);
	} else {
		mwl.insertHTML('#submissions', '<li>No one has responded yet.</li>')
	}
}

// Placeholder function for HTML templating
function template(str, obj){
	var i, regex;
	for(i in obj){
		regex = new RegExp('\{' + i + '\}', 'g');
		str = str.replace(regex, obj[i]);
	}
	return str;
}